package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.AgriProductMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.AgriProduct;
import com.example.demo.entity.Commodity;
import com.example.demo.entity.ProductInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.demo.base.ArrayCountUtils.merge;

@RestController
@RequestMapping("/agriProduct")
public class AgriProductController {
    @Resource
    AgriProductMapper agriProductMapper;

    /**
     * 获取数据库全部数据 √
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll(@RequestParam String tenantId) {
        QueryWrapper<AgriProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        return Result.success(agriProductMapper.selectList(queryWrapper));
    }

    /**
     * 分页获取数据库数据 √
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.eq(AgriProduct::getTenantId, Integer.valueOf(tenantId));
        Page<AgriProduct> agriProductPage = agriProductMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(agriProductPage);
    }

    /**
     * 农产品编号查找  √
     *
     * @param id
     * @return
     */

    @GetMapping("/findById")
    public Result<?> findById(@RequestParam String id,
                              @RequestParam String tenantId) {
        AgriProduct agriProduct = new AgriProduct();
        agriProduct.setAgriId(Integer.valueOf(id));
        agriProduct.setTenantId(Integer.valueOf(tenantId));
        AgriProduct tmp = agriProductMapper.selectByMultiId(agriProduct);
        List<AgriProduct> agriProductList = new ArrayList<>();
        if (tmp != null) {
            agriProductList.add(tmp);
            return Result.success(agriProductList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据农产品名称模糊查找   √
     *
     *@return
     */
    @GetMapping("/findByName")
    public Result<?> findByComName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam String name,
                                   @RequestParam String tenantId) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getName, name)
                .eq(AgriProduct::getTenantId,tenantId);
        Page<AgriProduct> agriProductPage = agriProductMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (agriProductPage.getTotal() == 0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(agriProductPage);
    }

    /**
     * 根据类别模糊查找
     *
     * @return
     */
    @GetMapping("/findByKind")
    public Result<?> findByKind(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam String kind,
                                @RequestParam String tenantId) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getKind, kind)
                .eq(AgriProduct::getTenantId,tenantId);
        Page<AgriProduct> agriProductPage = agriProductMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (agriProductPage.getTotal() == 0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(agriProductPage);
    }

    /**
     * 根据存储地址模糊查找 √
     *
     * @param add
     * @return
     */
    @GetMapping("/findByAdd")
    public Result<?> findByAdd(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String add,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getInventoryAdd, add)
                .eq(AgriProduct::getTenantId,tenantId);
        Page<AgriProduct> agriProductPage = agriProductMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (agriProductPage.getTotal() == 0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(agriProductPage);
    }
    /**
     * 农产品类别信息统计，根据类别与库存
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/chatData")
    public Result<?> chatData(@RequestParam String tenantId) {
        LambdaQueryWrapper<AgriProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgriProduct::getTenantId, tenantId);
        List<AgriProduct> agriProductList = agriProductMapper.selectList(queryWrapper);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (AgriProduct info : agriProductList) {
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(info.getKind());
            billsNums.setSums(Integer.valueOf((int) info.getInventory()));
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }

    /**
     * 删除 √
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String id,
                            @RequestParam String tenantId) {
        AgriProduct agriProduct = new AgriProduct();
        agriProduct.setAgriId(Integer.valueOf(id));
        agriProduct.setTenantId(Integer.valueOf(tenantId));
        int res = agriProductMapper.deleteByMultiId(agriProduct);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息          √
     *
     * @param id
     * @param name
     * @param kind
     * @param handleDate
     * @param inventory
     * @param inventoryAdd
     * @param deadline
     * @param note
     * @return
     */

    @GetMapping(value = "/add")
    public Result<?> saveA(@RequestParam String id,
                           @RequestParam String name,
                           @RequestParam String kind,
                           @RequestParam String handleDate,
                           @RequestParam String inventory,
                           @RequestParam String inventoryAdd,
                           @RequestParam String deadline,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (id.isEmpty() || name.isEmpty() || kind.isEmpty() || handleDate.isEmpty() || inventory.isEmpty() || inventoryAdd.isEmpty() || deadline.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        AgriProduct agriProduct = new AgriProduct(Integer.valueOf(id), name, kind, handleDate, Double.valueOf(inventory), inventoryAdd, deadline, note, Integer.valueOf(tenantId));
        agriProductMapper.insert(agriProduct);
        return Result.success();
    }

    /**
     * 根据农产品编号更新信息  √
     *
     * @param id
     * @param name
     * @param kind
     * @param handleDate
     * @param inventory
     * @param inventoryAdd
     * @param deadline
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String id,
                                @RequestParam String name,
                                @RequestParam String kind,
                                @RequestParam String handleDate,
                                @RequestParam String inventory,
                                @RequestParam String inventoryAdd,
                                @RequestParam String deadline,
                                @RequestParam String note,
                                @RequestParam String tenantId) {
        if (id.isEmpty() || name.isEmpty() || kind.isEmpty() || handleDate.isEmpty() || inventory.isEmpty() || inventoryAdd.isEmpty() || deadline.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        AgriProduct agriProduct = new AgriProduct(Integer.valueOf(id), name, kind, handleDate, Double.valueOf(inventory), inventoryAdd, deadline, note,Integer.valueOf(tenantId));
        agriProductMapper.updateByMultiId(agriProduct);
        return Result.success();
    }
}
